Use SearchEngine.getSearchParticipants() for non-Java language search - #3772
Use SearchEngine.getSearchParticipants() for non-Java language search#3772arcivanov wants to merge 1 commit into
Conversation
76d2bf7 to
67c3a2f
Compare
67c3a2f to
0a7a4c2
Compare
|
@arcivanov taking a look at this now. First thought is, is it possible for you to write and add a stubbed additional search participant to the tests so that we can verify that it's being invoked? eg. a kotlin one that returns a fixed result for a given test file name I know there's https://github.com/karellen/karellen-jdtls-kotlin, but something in the repo would be very helpful. Still need to try out https://github.com/karellen/karellen-jdtls-kotlin |
I can certainly work on Language X test with stubbed-out responses that exercises the API if that's something you'd like. |
0a7a4c2 to
cf684bb
Compare
|
@datho7561 Test added. |
|
Seems the code it snot compiling, it might just be a missing import though |
|
@datho7561 sorry about that, let me check |
cf684bb to
364456b
Compare
Replace getDefaultSearchParticipant() with getSearchParticipants() across
all jdtls search call sites so that contributed DerivedSourceSearchParticipants
(e.g., Kotlin, Scala) are included in reference, implementation, hover,
code lens, workspace symbol, and type hierarchy searches.
Call sites updated:
- ReferencesHandler.search()
- CodeLensHandler.findReferences()
- HoverInfoProvider.hasMatchInUnit()
- JDTUtils.findElementsAtSelection()
- ImplementationCollector (method + type implementations)
- TypeHierarchyHandler (supertypes + subtypes)
- WorkspaceSymbolHandler.search()
Includes a test search participant ("Language X") registered for .langx
files to verify that contributed participants are invoked during handler
searches.
364456b to
2e3e281
Compare
Summary
ReferencesHandler,CodeLensHandler,ImplementationCollector,HoverInfoProvider,WorkspaceSymbolHandler) to useSearchEngine.getSearchParticipants()instead of hardcoding only the default Java participantWorkspaceSymbolHandler.searchAllTypeNames()andsearchAllMethodNames()withsearch()calls through contributed participants — both APIs only query the default Java participant's indexes, making non-Java types and methods invisible to workspace/symbol queriesImplementationCollector.findTypeImplementations()with an IMPLEMENTORS search through contributed participants —newTypeHierarchy().getAllSubtypes()only discovers Java subtypesJDTUtils.resolveCompilationUnit(IFile)to query contributedSearchParticipant.getCompilationUnit(IFile)for non-Java source filesJDTUtils.findElementsAtSelection()to search contributed participants when Java'scodeSelect()returns empty — enables go-to-definition and hover for types/methods provided by non-Java languages (e.g., Kotlin facade classes and property accessors) from Java source filesReferencesHandlerwhen the search target itself is a non-Java element (contributed participants may not provide full accuracy information)SearchParticipantRegistryinstead of hardcoding"java"NavigateToDefinitionHandler.computeBreakContinue()withisJavaLikeFileNameto preventClassCastExceptionwhen ECJ'sASTParserattempts to parse a contributedICompilationUnit(e.g., Kotlin)NavigateToDeclarationHandlerto fall back to definition for non-METHOD elements (TYPE, FIELD) from non-Java files instead of returning null — enablestextDocument/declarationfor contributed languagesNavigateToTypeDefinitionHandlerto fall back to definition whenresolveElementType()returns null for non-Java files — enablestextDocument/typeDefinitionfor contributed languages whose declaring types have no Java counterpartBaseJDTLanguageServer.computeAsync()andJDTLanguageServer.computeAsyncWithClientProgress()instead of silently swallowing themSearchUtils.getContributedSearchParticipants()to centralize the "skip default participant at index 0" logic, fixing a bug where identity comparison againstgetDefaultSearchParticipant()never filtered anything (it creates a new instance per call)ContributedSearchRequestorinWorkspaceSymbolHandlerto deduplicate two near-identical inline anonymousSearchRequestorimplementationsMotivation
SearchEngine.search()already supports multipleSearchParticipantinstances, but all call sites in jdtls hardcode a single-element array containing only the default Java participant. This prevents non-Java languages from contributing search results for cross-language features.With the new
org.eclipse.jdt.core.searchParticipantextension point (eclipse-jdt/eclipse.jdt.core#4938), languages like Kotlin can register search participants that index.ktfiles and contribute to JDT's search infrastructure. This PR wires jdtls to use those contributed participants.Changes
1. Search call sites →
getSearchParticipants()ReferencesHandler,CodeLensHandler,ImplementationCollector, andHoverInfoProvider.isResolved()now callSearchEngine.getSearchParticipants()which returns[default, ...contributed].2.
WorkspaceSymbolHandler— contributed participant supplementationsearchAllTypeNames()andsearchAllMethodNames()only query indexes through the default Java search participant (seeBasicSearchEngineline 1987:getDefaultSearchParticipant(), // Java search only). Non-Java types and methods indexed by contributed participants are invisible to these APIs.After both the existing
searchAllTypeNamesandsearchAllMethodNamescalls, supplementarysearch()calls are now run through contributed participants only (excluding the default). They create TYPE/METHOD DECLARATIONS patterns with the same match rule (camelcase or wildcard) used by the workspace symbol query, and convertSearchMatchresults toSymbolInformationwith proper location, container name, and symbol kind. Both searches share a singleContributedSearchRequestorclass that handles bothITypeandIMethodelements viaIMember.3.
ImplementationCollector— contributed participant supplementationfindTypeImplementations()usestype.newTypeHierarchy().getAllSubtypes()which only discovers Java subtypes. For non-Java types (e.g., a Kotlin class implementing a Java/Kotlin interface), the type hierarchy is empty. After the standard hierarchy lookup, a supplementary IMPLEMENTORS search is now run through contributed participants (viaSearchUtils.getContributedSearchParticipants()) to discover non-Java implementations. Results are deduplicated by FQN against the types already found by the hierarchy.4.
SearchUtils.getContributedSearchParticipants()Centralizes the logic of obtaining contributed (non-default) search participants.
SearchEngine.getSearchParticipants()returns the default Java participant at index 0 followed by contributed participants; this utility returns only the contributed portion. Previously, call sites used identity comparison againstgetDefaultSearchParticipant()which never worked correctly —getDefaultSearchParticipant()creates a newJavaSearchParticipantinstance per call, so!=was always true and the default participant was never filtered out, causing duplicate results.5.
ReferencesHandler— accept inaccurate matches for non-Java elementsWhen the search target is a non-Java element (resolved via a contributed participant),
ReferencesHandlernow accepts inaccurate search matches. Contributed participants may not provide full accuracy information, and filtering these out would silently drop valid cross-language references.6.
resolveCompilationUnit(IFile)fallbackWhen a non-Java file (e.g.,
.kt) is opened, the method now queries contributed participants viagetCompilationUnit(IFile)as a fallback after the standard Java resolution path. This enables document symbols, hover, go-to-definition, call hierarchy, and code lenses for derived source languages.7.
findElementsAtSelection()search participant fallbackWhen Java's
codeSelect()returns empty (which happens for types and methods provided by non-Java languages), a newresolveViaSearchParticipants()helper extracts the identifier at the cursor and searches contributed participants for matching TYPE or METHOD declarations. This fixes Java→Kotlin navigation gaps (go-to-definition and hover on Kotlin facade classes and property accessors from Java files).8. Type hierarchy support for contributed participants
TypeHierarchyHandlercannot reconstitute aIJavaElementfrom its handle identifier (which happens for contributed elements), it falls back tocodeSelect()at the element's source position to re-resolve itresolveContributedSupertypes): For non-Java types, JDT'snewSupertypeHierarchy()does not work. Instead, the superclass and superinterface names are resolved viaSearchEnginesearch across all participants (hoisted to avoid N+1 array allocations), building the hierarchy items from the resolvedITypeinstancessupplementWithContributedSubtypes): After JDT computes subtypes via its own hierarchy, a secondary IMPLEMENTORS search is run against contributed participants only (viaSearchUtils.getContributedSearchParticipants()) to discover additional subtypes from non-Java languages9. Correct language ID for hover MarkedStrings
HoverInfoProvider.getLanguageId()queriesSearchParticipantRegistryfor the file extension and its associated LSP language ID, falling back to"java"for standard Java elements. This ensures hover popups render syntax highlighting for the correct language.10.
NavigateToDefinitionHandler— guard ECJ AST parsing for non-Java filescomputeBreakContinue()usesASTParser.createAST()to parse break/continue statements, which internally casts theICompilationUnitto ECJ'sorg.eclipse.jdt.internal.compiler.env.ICompilationUnit. Contributed compilation units (e.g.,KotlinCompilationUnit) do not implement this internal interface, causing aClassCastException. AddedJavaCore.isJavaLikeFileName()guard to skip break/continue resolution for non-Java files.11.
NavigateToDeclarationHandler— fallback for non-Java filestextDocument/declarationonly made sense for METHOD elements (finding the declaring method in the type hierarchy). For non-Java files, TYPE and FIELD elements were rejected before reaching the non-Java fallback. The check is now split: non-METHOD elements from non-Java files fall back tocomputeDefinitionNavigation(), while Java files preserve existing behavior (return null for non-METHOD elements).12.
NavigateToTypeDefinitionHandler— fallback for non-Java filestextDocument/typeDefinitionusesresolveElementType()which callsIType.resolveType(typeName)on the declaring type. For contributed types whose declaring type is a pure non-Java type with no Java counterpart,resolveType()returns null. Added fallback: when type resolution fails and the file is non-Java, delegate tocomputeDefinitionNavigation(element)to navigate to the element itself rather than returning nothing.13. Log unhandled exceptions in
computeAsync()BaseJDTLanguageServer.computeAsync()andJDTLanguageServer.computeAsyncWithClientProgress()now attach awhenComplete()handler that logs unhandled exceptions instead of silently swallowing them. This makes failures in LSP request handlers visible in the server log for diagnosis.Backward Compatibility
When no search participant extensions are registered,
getSearchParticipants()returns only the default Java participant, and the fallback paths only run when standard JDT resolution already returned empty. Behavior is identical to before.Test plan
SearchParticipantsTest(14 tests),WorkspaceSymbolHandlerTest(17 tests), andTypeHierarchyHandlerTest(4 tests) all pass. Tests verify:getSearchParticipants()API consistency (includes default, correct class, stable across calls)getCompilationUnit()returns nullresolveCompilationUnitfallback loop runs without error for non-Java files (returns null when no contributed participant is registered)resolveCompilationUnitstill works for Java filesMarkedStringlanguage ID is"java"for Java elementsfindElementsAtSelectionresolves Java elements and returns empty for whitespacecomputeBreakContinue)WorkspaceSymbolHandlerTestandTypeHierarchyHandlerTesttests pass (no regressions)